<?php
header( 'Content-Type: application/json');
session_start();
require_once('../../lib/JSON.php');
require_once('../../config/connection.php');
require_once('../../utils/CommonUtils.php');
require_once('../../utils/DBUtils.php');
require_once('../../application/model/BaseDAO.php');
require_once("../../lib/core/Session.php");
require_once("../../lib/core/Object.php");
$commonUtils = new CommonUtils();
$baseDAO = new BaseDAO();
$returnRes = array();

$page = $commonUtils->getParameter("page");
if (!isset($page) || $page == '') {
	$page = "list";
}

$action = $commonUtils->getParameter("crudaction");
$sortDirection = $commonUtils->getParameter("dir", "ASC");

$id = $commonUtils->getParameter("id");
$name = $commonUtils->getParameter("name");
$note = $commonUtils->getParameter("note");
$orderdisplay = $commonUtils->getParameter("orderdisplay");

if ($page == "list") {
	$sortExpression = $commonUtils->getParameter("sort", "name");
	$begin = $commonUtils->getParameter("pg4_offset", 0);
	$limit = $commonUtils->getParameter("maxPageItems", 10);
	
	// xóa record
	if (isset($action) && $action  == 'delete') {
		$checkList = $commonUtils->getParameter("checkList");
		if (isset($action) && $action  == 'delete') {
			$checkList = $commonUtils->getParameter("checkList");
			$ids = explode("|", $checkList);
			$returnRes['ids'] = $checkList;
			if (isset($ids) && is_array($ids)) {
				foreach($ids as $id) {
					$baseDAO->del("role", $id);
				}	
			}
		}
	}
	
	$query = " 1 = 1";
	if (isset($name) && $name != '') {
		$query .= " AND name LIKE '%" . $name . "%'";
	}
	if (isset($sortExpression) && $sortExpression != '') {
		$orderby = " ORDER BY $sortExpression $sortDirection";
	}
	
	$items = $baseDAO->getItems("role", array("id", "name", "note"), $query, $orderby, $begin, $limit);
	$totalItems = $baseDAO->getTotalItems("role", $query);
	$returnRes['listResult'] = $items;
	$returnRes['totalItems'] = $totalItems;
	$returnRes['data'] = array('crudaction' => 'search');
	
}else if($page == 'edit'){
	$returnRes["messageResponse"] = $action;
	if (isset($action) && $action == 'insert-update') {
		if (isset($id) && $id > 0) {
			$values = array($id, $name, $note);
						
			$res = $baseDAO->update("role", array("id", "name", "note"), $values);
			if (isset($res) && $res >= 1) {
				$returnRes["messageResponse"] = "Quá trình cập nhật thành công";
			}else{
				$returnRes["messageResponse"] = "Quá trình cập nhật không thành công";
			}
		}else{
			$values = array($name);
			$existItem = $baseDAO->getItem("role", array("name"), $name, "name");
			if($existItem["name"] != null && $existItem["name"] != ""){ // check dublicate
				$returnRes["messageResponse"] = "Quyền này đã tồn tại";
			}else{
				$res = $baseDAO->create("role", array("name"), $values);
				if (isset($res) && $res >= 1) {
					$returnRes["messageResponse"] = "Thêm mới thành công";
				}else{
					$returnRes["messageResponse"] = "Thêm mới không thành công";
				}	
			}
		}
	}
	$item = $baseDAO->getItem("role", array("id", "name", "note"), $id);
	$returnRes['data'] = $item;
}

$returnRes['success'] = true;
$jsonUtil = new ext_Json();
echo $jsonUtil->encode($returnRes);

?>